
/* GENERATED CODE:
 *  Any modifications you make are guaranteed
 *  to be deleted when this is regenerated.
 * Please edit "PROTOCOLS" inside this directory
 *
 * Names are all upper case because of "String" and "string."
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using OWED.Network.Messages.General;
using OWED.Network.Messages;
using OWED.Network.Messages.Reply;


namespace OWED.Network.Messages.General
{
	public class Comment: IProtocolItem 
	{
		User my_user;
		DiscussionID my_discussionid;
		Date my_date;
		Text my_text;
		int my_id;
		public Comment (int id, User user, DiscussionID discussionid, Date date, Text text)
		{
			my_id = id;
			my_user = user;
			my_discussionid = discussionid;
			my_date = date;
			my_text = text;
		}
		public User User { get { return my_user; } }
		public DiscussionID DiscussionID { get { return my_discussionid; } }
		public Date Date { get { return my_date; } }
		public Text Text { get { return my_text; } }
		public int id { get { return my_id; } }


		public static new Comment FromXml(XmlDocument xml)
		{
			int id = int.Parse(xml.SelectSingleNode("Comment").Attributes.GetNamedItem("id").Value);
			XmlDocument doc;
			if (xml.SelectSingleNode("Comment/User") != null &&
					xml.SelectSingleNode("Comment/DiscussionID") != null &&
					xml.SelectSingleNode("Comment/Date") != null &&
					xml.SelectSingleNode("Comment/Text") != null) {
				doc = new XmlDocument();
				doc.AppendChild(doc.ImportNode(xml.SelectSingleNode("Comment/User"), true));
				User user = User.FromXml(doc);

				doc = new XmlDocument();
				doc.AppendChild(doc.ImportNode(xml.SelectSingleNode("Comment/DiscussionID"), true));
				DiscussionID discussionid = DiscussionID.FromXml(doc);

				doc = new XmlDocument();
				doc.AppendChild(doc.ImportNode(xml.SelectSingleNode("Comment/Date"), true));
				Date date = Date.FromXml(doc);

				doc = new XmlDocument();
				doc.AppendChild(doc.ImportNode(xml.SelectSingleNode("Comment/Text"), true));
				Text text = Text.FromXml(doc);

				return new Comment(id, user, discussionid, date, text);
			}
			throw new ConnectionException("Bad document supposedly of type:Comment");
		}


		public override XmlDocument ToXml()		{
			XmlDocument doc = new XmlDocument();

			doc.AppendChild(doc.CreateElement("Comment"));
			XmlAttribute attr;
			attr = doc.CreateAttribute("id");
			attr.Value = my_id.ToString();
			doc.FirstChild.Attributes.Append(attr);


			if (my_user != null) {
				XmlNode node;
				node = doc.ImportNode(my_user.ToXml().FirstChild, true);
				doc.FirstChild.AppendChild(node);
			}

			if (my_discussionid != null) {
				XmlNode node;
				node = doc.ImportNode(my_discussionid.ToXml().FirstChild, true);
				doc.FirstChild.AppendChild(node);
			}

			if (my_text != null) {
				XmlNode node;
				node = doc.ImportNode(my_text.ToXml().FirstChild, true);
				doc.FirstChild.AppendChild(node);
			}

			if (my_date != null) {
				XmlNode node;
				node = doc.ImportNode(my_date.ToXml().FirstChild, true);
				doc.FirstChild.AppendChild(node);
			}

			return doc;
		}
	}
}
